<template>
	<view>
		<view class="address">
			<view class="divContent">
				<u-empty :show="show"  text="地址为空" mode="data" marginTop="50%" icon="http://cdn.uviewui.com/uview/empty/data.png">
				</u-empty>
				
				<u-swipe-action>
					<u-swipe-action-item 
						v-for="(item,index) in addressList" 
						:key="index"
						:options="getSwipeOptions(item)"
						@click="handleSwipeClick"
						:name="index"
					>
						<view class="divItem">
							<view class="divItemContent" @click="onItemTap(item)">
								<view class="divAddress">
									<text
										:class="{spanNone:item.label === '无',spanCompany:item.label === '公司',spanHome:item.label === '家',spanSchool:item.label === '学校'}">{{item.label}}</text>
									{{item.detail}}
								</view>
								<view class="divUserPhone">
									<text>{{item.consignee}}</text>
									<text>{{item.gender === 0 ? '先生' : '女士'}}</text>
									<text>{{item.phone}}</text>
								</view>
								<view class="divDefault" v-if="item.isDefault === 1">
									<text class="defaultText">默认地址</text>
								</view>
							</view>
							<view class="divActions">
								<view class="editBtn" @click="toAddressEditPage(item)">编辑</view>
								<view class="defaultBtn" @click="setDefaultAddress(item)" v-if="item.isDefault !== 1">
									设为默认
								</view>
							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action>
			</view>
			<view class="divBottom" @click="toAddressCreatePage">+ 新增收货地址</view>
		</view>
	</view>
</template>

<script>
	import regeneratorRuntime, {
		async
	} from '../../../lib/runtime/runtime';
	import {addressFindOneApi,updateAddressApi,addAddressApi,deleteAddressApi,addressListApi,setDefaultAddressApi} from '../../../api/address.js'
	export default {
		data() {
			return {
				show:true,
				addressList: [],
				mode: ''
			}
		},
		mounted() {},
		onShow() {
			this.initData()
		},
		onLoad(options){
			this.mode = (options && options.mode) || ''
		},
		methods: {
			toAddressEditPage(item) {
				uni.navigateTo({
					url:"/subpkg/pages/addressEdit/addressEdit?id="+item.id,
				})				
			},
			toAddressCreatePage() {
				uni.navigateTo({
					url:'/subpkg/pages/addressEdit/addressEdit'
				})
			},
			async initData() {
				const res = await addressListApi()
				if (res.code === 0) {				
					this.addressList = res.data
					if(this.addressList.length > 0){
						this.show = false
					}
				} else {				
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			async setDefaultAddress(item) {
				if (item.id) {
					uni.showLoading({
						title: '设置中...'
					})
					const res = await setDefaultAddressApi({
						id: item.id
					})
					uni.hideLoading()
					if (res.code === 0) {
						uni.showToast({
							title: '设置成功',
							icon: 'success'
						})
						this.initData()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}
			},
			// 获取左滑操作按钮配置
			getSwipeOptions(item) {
				const options = [
					{
            text: '删除',
            style: {
              backgroundColor: '#f44336',
              color: '#ffffff'
            }
					}
				]
				return options
			},
			// 处理左滑按钮点击
			async handleSwipeClick(e) {
				const { index, name } = e
				const item = this.addressList[name]
				if (!item) return
				// 当前仅一个删除按钮
				await this.deleteAddress(item)
			},
			// 删除地址
			async deleteAddress(item) {
				uni.showModal({
					title: '确认删除',
					content: '确认要删除这个地址吗？',
					success: async (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '删除中...'
							})
							
							const result = await deleteAddressApi({
								id: item.id
							})
							
							uni.hideLoading()
							
							if (result.code === 0) {
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								})
								this.initData()
							} else {
								uni.showToast({
									title: result.msg,
									icon: 'none'
								})
							}
						}
					}
				})
			},
			onItemTap(item) {
				if(this.mode === 'select'){
					// 选择模式：将选中的地址返回到上一个页面
					let pages = getCurrentPages()
					let prevPage = pages[pages.length - 2]
					prevPage.setData({
						address:item
					})
					uni.navigateBack()
				}else{
					// 正常模式：进入编辑页
					this.toAddressEditPage(item)
				}
			}
		}
	}
</script>

<style>
	@import url(./address.css);
	page{
		background-color:  #f3f2f7;
	}
</style>
